﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using AMM.Business.Finance;
using AMM.Business.Store;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.Model;
using AMM.Model.Finance;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class CostEditControl : BaseEditControl {

        private FlexBindingSource<Cost> _fbs;

        public override object BaseEditModel {
            get { return this._fbs.CurrentItem; }
            set {
                if (this._fbs == null) { this._fbs = new FlexBindingSource<Cost>(); }
                this._fbs.CurrentItem = (Cost)value;
            }
        }

        public CostEditControl() {
            this.InitializeComponent();
        }

        private void CostEditControl_Load(object sender, EventArgs e) {
            if (this._fbs == null) { this._fbs = new FlexBindingSource<Cost>(); }
            this.BindCostType();
            this.BindBankAccount();
            this.BindVendor();
            this.BindStatement();
            this.CreateFormBindings();
            this.BindValidation();

            this.cmbAccount.SelectedIndex = 0;
        }

        private void BindCostType() {
            List<object> resultList = new List<object>();
            resultList.Add(new { Text = "", Value = 0 });
            resultList.Add(new { Text = "收入", Value = 1 });
            resultList.Add(new { Text = "支出", Value = 2 });

            this.cmbCostType.DataSource = resultList;
            this.cmbCostType.DisplayMember = "Text";
            this.cmbCostType.ValueMember = "Value";
        }

        private void CreateFormBindings() {
            this._fbs.CreateBinding(this.cmbCostType, x => x.SelectedValue, y => y.CostType);
            this._fbs.CreateBinding(this.tbAccountNumber, x => x.Text, y => y.AccountNumber, DataSourceUpdateMode.OnPropertyChanged);
            this._fbs.CreateBinding(this.tbAmount, x => x.Text, y => y.Amount);
            this._fbs.CreateBinding(this.ddlStatement, x => x.SelectedValueEx, y => y.StatementId);
            this._fbs.CreateBinding(this.tbRemarks, x => x.Text, y => y.Remarks);
        }

        private void BindValidation() {
            this.ValidatorForEdit.SetType(this.tbAccountNumber, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbAccountNumber, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbAccountNumber, "^[A-Za-z0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbAccountNumber, "帐号只能由字母，数字组成");

            this.ValidatorForEdit.SetType(this.tbAmount, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbAmount, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbAmount, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbAmount, "金额只能由数字组成");

            this.ValidatorForEdit.SetType(this.cmbCostType, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbCostType, this.RequiredMessage);
        }

        private void BindStatement() {
            StatementSearchModel searchModel = new StatementSearchModel() { Status = 2 };
            StatementProcessor statementProcessor = new StatementProcessor(int.MaxValue);
            List<StatementViewModel> statementList = statementProcessor.FetchStatementViewModelList(searchModel);

            this.ddlStatement.DisplayMemberEx = "StatementCode";
            this.ddlStatement.ValueMemberEx = "Key";
            this.ddlStatement.HeightEx = 300;
            this.ddlStatement.WidthEx = 400;
            this.ddlStatement.PageSizeEx = 20;
            this.ddlStatement.DataBind(new StatementViewModel(), x => new { x.StatementCode, x.VendorCode, x.VendorName }, statementList);
        }

        private void BindBankAccount() {
            BankAccountSearchModel searchModel = new BankAccountSearchModel();
            BankAccountProcessor bankAccountProcessor = new BankAccountProcessor(int.MaxValue);
            List<BankAccountViewModel> bankAccountList = bankAccountProcessor.FetchBankAccountViewModelList(searchModel);

            this.ddlBankAccount.DisplayMemberEx = "BankAccountNumber";
            this.ddlBankAccount.ValueMemberEx = "BankAccountNumber";
            this.ddlBankAccount.HeightEx = 300;
            this.ddlBankAccount.WidthEx = 400;
            this.ddlBankAccount.PageSizeEx = 20;
            this.ddlBankAccount.DataBind(new BankAccountViewModel(), x => new { x.Code, x.BankAccountName, x.BankAccountNumber }, bankAccountList);
        }

        private void BindVendor() {
            VendorSearchModel searchModel = new VendorSearchModel() { IsEnabled = true };
            VendorProcessor vendorProcessor = new VendorProcessor(int.MaxValue);
            List<VendorViewModel> vendorList = vendorProcessor.FetchVendorViewModelList(searchModel);

            this.ddlVendor.DisplayMemberEx = "AccountNumber";
            this.ddlVendor.ValueMemberEx = "AccountNumber";
            this.ddlVendor.HeightEx = 300;
            this.ddlVendor.WidthEx = 400;
            this.ddlVendor.PageSizeEx = 20;
            this.ddlVendor.DataBind(new VendorViewModel(), x => new { x.Code, x.Name, x.AccountNumber }, vendorList);
        }

        private void cmbAccount_SelectedIndexChanged(object sender, EventArgs e) {
            switch (this.cmbAccount.SelectedIndex) {
                case 0:
                    this.tbAccountNumber.Enabled = true;
                    this.ddlBankAccount.Enabled = false;
                    this.ddlVendor.Enabled = false;
                    this.tbAccountNumber.Focus();
                    break;
                case 1:
                    this.tbAccountNumber.Enabled = false;
                    this.ddlBankAccount.Enabled = true;
                    this.ddlVendor.Enabled = false;
                    this.ddlBankAccount.Focus();
                    break;
                case 2:
                    this.tbAccountNumber.Enabled = false;
                    this.ddlBankAccount.Enabled = false;
                    this.ddlVendor.Enabled = true;
                    this.ddlVendor.Focus();
                    break;
            }
        }

        private void ddlBankAccount_ValueChanged(object sender, EventArgs e) {
            this.tbAccountNumber.Text = Convert.ToString(this.ddlBankAccount.SelectedValueEx);
        }

        private void ddlVendor_ValueChanged(object sender, EventArgs e) {
            this.tbAccountNumber.Text = Convert.ToString(this.ddlVendor.SelectedValueEx);
        }
    }
}
